package com.xyw.code.common.core.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/8/2
 * Time: 下午10:41
 * Email: 1328312923@qq.com
 * 向前端返回时将Long转成字符串
 **/
public class LongListJsonSerializer extends JsonSerializer<List<Long>> {

    @Override
    public void serialize(List<Long> longList, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(longList!=null){
            List<String> ids = new ArrayList<>();
            longList.forEach(id->{
                ids.add((id == null ? null : id.toString()));
            });
            jsonGenerator.writeString( JSONObject.toJSONString(ids));
        }
    }
}
